package com.scale.service.voice.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import java.time.LocalDateTime;

/**
 * WebSocket消息统一格式
 * @author crp
 * @since 2025-09-28
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class WebSocketMessage {
    
    /**
     * 消息类型
     */
    private String type;           // PROGRESS_UPDATE, TASK_COMPLETED, TASK_FAILED, CONNECTED
    
    /**
     * 任务ID（CONNECTED消息可为空）
     */
    private String taskId;
    
    /**
     * 消息数据
     */
    private Object data;
    
    /**
     * 简单消息（用于CONNECTED）
     */
    private String message;
    
    /**
     * 用户ID
     */
    private String userId;
    
    /**
     * 时间戳
     */
    private String timestamp;
    
    public WebSocketMessage() {
        this.timestamp = LocalDateTime.now().toString();
    }
    
    public WebSocketMessage(String type, String taskId, Object data) {
        this();
        this.type = type;
        this.taskId = taskId;
        this.data = data;
    }
    
    public WebSocketMessage(String type, String message, String userId) {
        this();
        this.type = type;
        this.message = message;
        this.userId = userId;
    }
    
    /**
     * 创建连接成功消息
     */
    public static WebSocketMessage connected(String userId, String message) {
        return new WebSocketMessage("CONNECTED", message, userId);
    }
    
    /**
     * 创建进度更新消息
     */
    public static WebSocketMessage progressUpdate(String taskId, Object data) {
        return new WebSocketMessage("PROGRESS_UPDATE", taskId, data);
    }
    
    /**
     * 创建任务完成消息
     */
    public static WebSocketMessage taskCompleted(String taskId, Object data) {
        return new WebSocketMessage("TASK_COMPLETED", taskId, data);
    }
    
    /**
     * 创建任务失败消息
     */
    public static WebSocketMessage taskFailed(String taskId, Object data) {
        return new WebSocketMessage("TASK_FAILED", taskId, data);
    }
}
